
function UrlSearch() 
{
   var name,value; 
   var str=location.href; //取得整个地址栏
   var num=str.indexOf("?") 
   str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
   var arr=str.split("&"); //各个参数放到数组里
   for(var i=0;i < arr.length;i++){ 
    num=arr[i].indexOf("="); 
    if(num>0){ 
     name=arr[i].substring(0,num);
     value=arr[i].substr(num+1);
     this[name]=value;
     } 
    } 
} 
var Request=new UrlSearch(); //实例化
//alert(Request.billCode);
var billCode = Request.billCode;
var ladingBillCode = Request.ladingBillCode;
var shipperName = Request.shipperName;

//var feeStoreOut = Ext.create('Ext.data.Store', {
//	fields: ['ee', 'qq'],
//	data : [
//		{"ee":"", "qq":"货代板块"},
//		{"ee":"1", "qq":"船代板块"},
//		{"ee":"0", "qq":"船务板块"}
//	]
//});
var storeOut = Ext.create('Ext.data.Store', {
    fields: ["ID",
             "SETTLEMENT_UNIT",
             "COST_NAME",
             "CURRENCY",
             "CONTAINER_TYPE",
             "TAX_PRICE",
             "CONTAINER_NUM",
             "AMOUNT",
             "EXCHANGE_RATE",
             "EQUIVALENT_RMB",
             "REMARK",
             "COST_TYPE",
             "COST_PLATE",
             "INPUTER"],
    pageSize: 15,  //页容量5条数据
    //是否在服务端排序 （true的话，在客户端就不能排序）
    remoteSort: false,
    remoteFilter: true,
    proxy: {
        type: 'ajax',
        url: '../consignHandle/copeListByBillCode?billCode='+billCode+"&ladingBillCode="+ladingBillCode,
        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
            type: 'json', //返回数据类型为json格式
            root: 'result.list',  //数据
            totalProperty: 'result.total' //数据总条数
        }
    },
    sorters: [{
        property: 'ordeId',
        direction: 'desc'
    }],
//    autoLoad: true
});
storeOut.load({
    callback :function(r,options,success){  
        if(success){  
        	allAmountOut();
        }  
    }  
    });
var store1 = Ext.create('Ext.data.Store', {
    fields: ["Fee",
           ],
    pageSize: 15,  //页容量5条数据
    //是否在服务端排序 （true的话，在客户端就不能排序）
    remoteSort: false,
    remoteFilter: true,
    proxy: {
        type: 'ajax',
        url: '../consignHandle/findAmount',
        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
            type: 'json', //返回数据类型为json格式
            root: 'amount',  //数据
        }
    },
    sorters: [{
        property: 'ordeId',
        direction: 'desc'
    }],
    autoLoad: true
});
var gridOut = Ext.create('Ext.grid.Panel', {
    //renderTo: "output",
//	renderTo: "output",
    store: storeOut,
    //height: document.documentElement.clientHeight,
    height:150,
    stripeRows:true,
    columnLines: true,
    selModel:Ext.create('Ext.selection.CheckboxModel',{mode:"SIMPLE"}),
    columns: [
              {text: 'ID', dataIndex: 'ID', align: 'left', hidden:true,maxWidth: 100 },
        {text: '结算单位', dataIndex: 'SETTLEMENT_UNIT', align: 'left', maxWidth: 100 },
        {text: '费用名称', dataIndex: 'COST_NAME', maxWidth: 200 },
        {text: '币种', dataIndex: 'CURRENCY', maxWidth: 80},
        {text: '箱型', dataIndex: 'CONTAINER_TYPE', maxWidth: 80},
        {text: '单价', dataIndex: 'TAX_PRICE', maxWidth: 80, render:convertDate},
        {text: '数量', dataIndex: 'CONTAINER_NUM', align: 'left', minWidth: 80},
        {text: '应付金额', dataIndex: 'AMOUNT', maxWidth: 80},
        {text: '比率', dataIndex: 'EXCHANGE_RATE', maxWidth: 80},
        {text: '折合人民币', dataIndex: 'EQUIVALENT_RMB', maxWidth: 80},
        {text: '备注', dataIndex: 'REMARK', maxWidth: 80 , render:convertDate},
        {text: '对账', dataIndex: 'forwarderCustomerService', maxWidth: 80},
        {text: '审核', dataIndex: 'transportClause', maxWidth: 80},
        {text: '开票', dataIndex: 'boxTypeConfirm', maxWidth: 80},
        {text: '收款', dataIndex: 'chineseName', maxWidth: 80},
        {text: '费用类型', dataIndex: 'COST_TYPE', maxWidth: 80},
        {text: '费用板块', dataIndex: 'COST_PLATE', maxWidth: 80},
        {text: '录入人', dataIndex: 'INPUTER', maxWidth: 80}
    ],
//    bbar: [{
//        xtype: 'pagingtoolbar',
//        store: storeOut,
//        displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
//        emptyMsg: "没有数据",
//        beforePageText: "当前页",
//        afterPageText: "共{0}页",
//        displayInfo: true
//    }],
    listeners: {
        'itemclick': function (view, record, item, index, e) {
//                    Ext.MessageBox.alert("标题", record.data.cataId);
        }
    }
});

function convertDate(val) {
    return Ext.util.Format.date(new Date(val), 'Y-m-d');
}

var tradeTypeStoreOut = Ext.create('Ext.data.Store', {
    fields: ['tt', 'vv'],
    data : [
        {"tt":"", "vv":"全部"},
        {"tt":"AK", "vv":"内贸"},
        {"tt":"AZ", "vv":"外贸"}
    ]
});

var c1Out = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 60 },
    items: [
        {xtype: 'combobox',  name: 'startDate12', id:'startDate12',columnWidth:  1/5,padding: '5px', editable: false,fieldLabel: '费用板块',store:feeStore,displayField: 'name'},
        {xtype: 'combobox', name: 'startDate13',id:'startDate13',columnWidth: 1/5,padding: '5px',editable: false,fieldLabel: '费用名称',store:FeeNamestore,displayField:'FEE_NAME'
//        	,listeners:{'select':function(combo,record,index){
//        	Ext.getCmp('startDate3').setValue(decodeURI(shipperName));
//        	store1.on('beforeload', function(store,options) {  
//				
//	            var new_params1 = {  
//	            		BILL_CODE:encodeURI(billCode),
//	            		COST_NAME:encodeURI(record[0].data.FEE_NAME)
//	            };  
//		//这里apply一下实现新的proxy  
//	            Ext.apply(store1.proxy.extraParams,new_params1);  
//	        });  
//			store1.load({callback :function(r,options,success){  
//				var tt= store1.getAt(0).get('Fee');
//				Ext.getCmp("startDate6").setValue(tt);
//		    }  }); 
//        	}
//        },
        },
        {xtype: 'combobox', name: 'startDate88', hidden:true,id:'startDate88',padding: '5px',editable: false, fieldLabel: '币种1',value:shipperName},
        {xtype: 'combobox', name: 'startDate14', id:'startDate14',columnWidth:  1/5,padding: '5px',editable: false, fieldLabel: '币种',store:bizhongStore,displayField: 'BIZHONG'},
        {xtype: 'combobox', name: 'startDate15',id:'startDate15',columnWidth: 1/5,padding: '5px',editable: false,fieldLabel: '结算单位',store:settlementstore,displayField: 'ENTITIES_FULLNAME',valueField:'IS_INNER_SETTLEMENT'},
        {xtype: 'combobox',  name: 'startDate16',id:'startDate16',value:'',editable: false,displayField: 'BOX_TYPE_NAME', queryMode: 'local',store:boxTypeStore,columnWidth:  1/5,padding: '5px', fieldLabel: '箱型'}
    ]
});
var c2Out = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 60 },
    items: [
        {xtype: 'textfield', name: 'startDate17', id:'startDate17',columnWidth: 1/4,padding: '5px', fieldLabel: '单价'
//        	,listeners:{
//            blur: function(){
//            	if(Ext.getCmp("startDate6").getValue()==null ||Ext.getCmp("startDate6").getValue()==""||Ext.getCmp("startDate6").getValue()=="undefined"){
//            		//把值set进应收单价
//            		Ext.getCmp("startDate6").setValue(Ext.getCmp("startDate17").getValue());
//            	}else{
//            		return;
//            	}
//            } } 
        	},
        	{xtype: 'textfield', name: 'startDate98', id: 'startDate98',hidden:true,padding: '5px', fieldLabel: 'ID'},
        {xtype: 'textfield', name: 'startDate18',id:'startDate18',columnWidth:  1/4,padding: '5px',fieldLabel: '数量'},
        {xtype: 'textfield', name: 'startDate19',id:'startDate19',columnWidth:  1/4,padding: '5px',fieldLabel: '比率'},
        {xtype: 'textfield', name: 'startDate20',id:'startDate20',columnWidth:  1/4,padding: '5px',fieldLabel: '备注'}
    ]
});
var cc4 = Ext.create('Ext.container.Container', {
    layout: 'column',
	defaults: { labelAlign: 'left', labelWidth: 60 },
    items: [
        {xtype: 'textfield', name: 'startDate62', id: 'startDate621',columnWidth: 1/5,padding: '5px',labelWidth: 70, fieldLabel: '合计人民币'},
        {xtype: 'textfield', name: 'startDate72',id: 'startDate721',columnWidth:  1/5,padding: '5px',labelWidth: 70,fieldLabel: '合计美元'},
        {xtype: 'textfield', name: 'startDate92',id: 'startDate921',columnWidth:  1/5,padding: '5px',labelWidth: 90,fieldLabel: '合计折合人民币'},
//        {xtype: 'textfield', name: 'startDate4',id: 'startDate4',columnWidth:  1/5,padding: '5px',fieldLabel: '备注'},
    ]
});
var bsOut = Ext.create('Ext.container.Container', {
    layout: 'form',
    items: [
//        {layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 2/5,   padding: '10px'},
        {layout : 'column',baseCls : 'my-panel-no-border',columnWidth: 1/5,  padding: '10px',  items: [
{xtype: "button",minWidth:70,icon:'../plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png', text: "增加应收费用",handler:function(){
	var COST_PLATE = Ext.getCmp('startDate12').getValue();
		var COST_NAME = Ext.getCmp('startDate13').getValue();
		var CURRENCY = Ext.getCmp('startDate14').getValue();
		var SETTLEMENT_UNIT = Ext.getCmp('startDate15').getRawValue();
		alert(SETTLEMENT_UNIT)
		var CONTAINER_TYPE = Ext.getCmp('startDate16').getValue();
		var TAX_PRICE = Ext.getCmp('startDate17').getValue();
		var CONTAINER_NUM = Ext.getCmp('startDate18').getValue();
		var EXCHANGE_RATE = Ext.getCmp('startDate19').getValue();
		var REMARK = Ext.getCmp('startDate20').getValue();
		var IS_INNER_SETTLEMENT = Ext.getCmp('startDate15').getValue();
		var BILL_CODE= billCode;
		var LADING_BILL_CODE= ladingBillCode;
		var shipperName = Ext.getCmp('startDate88').getValue();
		if((COST_PLATE==null || COST_PLATE=="")&&(COST_NAME==null || COST_NAME=="")&&(CURRENCY==null || CURRENCY=="")&&(SETTLEMENT_UNIT==null || SETTLEMENT_UNIT=="")&&(CONTAINER_TYPE==null || CONTAINER_TYPE=="")&&(TAX_PRICE==null||TAX_PRICE=="")&&(CONTAINER_NUM==null||CONTAINER_NUM=="")&&(EXCHANGE_RATE==null||EXCHANGE_RATE=="")&&(REMARK==null||REMARK=="")){
  			Ext.MessageBox.alert("提示","请填写数据");
  			return false;
  		}
		 Ext.Ajax.request({
			 url: '../consignHandle/saveFeeCopeInfo',
			 params:{COST_PLATE:COST_PLATE,COST_NAME:COST_NAME,CURRENCY:CURRENCY,
				 SETTLEMENT_UNIT:SETTLEMENT_UNIT,CONTAINER_TYPE:CONTAINER_TYPE,
				 TAX_PRICE:TAX_PRICE,CONTAINER_NUM:CONTAINER_NUM,EXCHANGE_RATE:EXCHANGE_RATE,REMARK:REMARK,
				 BILL_CODE:BILL_CODE,LADING_BILL_CODE:LADING_BILL_CODE,shipperName:shipperName,IS_INNER_SETTLEMENT:IS_INNER_SETTLEMENT
			 },
			 method: 'POST',
			success: function () {
               Ext.MessageBox.alert("保存成功","保存成功");
//                   var grid = Ext.getCmp('grid');
				Ext.getCmp("startDate12").setValue(null); 
				Ext.getCmp("startDate13").setValue(null); 
				Ext.getCmp("startDate14").setValue(null); 
				Ext.getCmp("startDate15").setValue(null); 
				Ext.getCmp("startDate16").setValue(null); 
				Ext.getCmp("startDate17").setValue(null); 
				Ext.getCmp("startDate18").setValue(null);  
				Ext.getCmp("startDate19").setValue(null); 
				Ext.getCmp("startDate20").setValue(null); 
				
				
				 gridOut.store.reload();
				 grid.store.reload();
           },
		 });
} },
{xtype: "button",minWidth:70,icon:'../plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png', text: "删除应收费用",handler:function(){
		var records = gridOut.getSelectionModel().getSelection();
			if(records.length==0){
				Ext.Msg.alert("提示","请选中需要删除的记录");return;
			}
			var arr = [];
			Ext.each(records,function(item){
				obj = {
						ID:item.data.ID
				};
				arr.push(obj);
			});
			var array = JSON.stringify(arr);
			Ext.Msg.confirm("提示","确定删除数据?",function(btn){
   				
   					Ext.Ajax.request({
   						url:'../consignHandle/deleteCopeFee',
   						params:{
   							array:array
   						},
   						timeout:5000,
   						success:function(response){
   							storeOut.load();
   							Ext.Msg.alert("成功","删除成功!");
   						},
   						failure:function(response){
   							Ext.Msg.alert("错误","请求异常!");
   						}
   					});
   				
   			});
	}  },
	 {xtype: "button",minWidth:70,icon:'../plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png', text: "修改应收费用",handler:function(){
  		var records = gridOut.getSelectionModel().getSelection();
			if(records.length==0){
				Ext.Msg.alert("提示","请选中需要修改的记录");return;
			}
			if(records.length>1){
				Ext.Msg.alert("提示","只能选中一条记录修改");return;
			}

			  Ext.getCmp('startDate98').setValue(records[0].data.ID);
			
	     	Ext.getCmp('startDate12').setValue(records[0].data.COST_PLATE);
   		Ext.getCmp('startDate13').setValue(records[0].data.COST_NAME);
   		Ext.getCmp('startDate14').setValue(records[0].data.CURRENCY);
   		Ext.getCmp('startDate15').setValue(records[0].data.SETTLEMENT_UNIT);
   		Ext.getCmp('startDate16').setValue(records[0].data.CONTAINER_TYPE);
   		Ext.getCmp('startDate17').setValue(records[0].data.TAX_PRICE);
   		Ext.getCmp('startDate18').setValue(records[0].data.CONTAINER_NUM);
   		Ext.getCmp('startDate19').setValue(records[0].data.EXCHANGE_RATE);
   		Ext.getCmp('startDate20').setValue(records[0].data.REMARK);
//   		var BILL_CODE= billCode;
//   		var LADING_BILL_CODE= ladingBillCode;
     }
     
			
			
			
  	 },{xtype: "button",minWidth:70,icon:'../plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png', text: "保存修改应收费用",handler:function(){
  		var ID = Ext.getCmp('startDate98').getValue();
  		var COST_PLATE = Ext.getCmp('startDate12').getValue();
		var COST_NAME = Ext.getCmp('startDate13').getValue();
		var CURRENCY = Ext.getCmp('startDate14').getValue();
		var SETTLEMENT_UNIT = Ext.getCmp('startDate15').getValue();
		var CONTAINER_TYPE = Ext.getCmp('startDate16').getValue();
		var TAX_PRICE = Ext.getCmp('startDate17').getValue();
		var CONTAINER_NUM = Ext.getCmp('startDate18').getValue();
		var EXCHANGE_RATE = Ext.getCmp('startDate19').getValue();
		var REMARK = Ext.getCmp('startDate20').getValue();
   		var BILL_CODE= billCode;
   		var LADING_BILL_CODE= ladingBillCode;
   		 Ext.Ajax.request({
    			 url: '../consignHandle/updateCopeInfo',
     			 params:{ID:ID,COST_PLATE:COST_PLATE,COST_NAME:COST_NAME,CURRENCY:CURRENCY,
     				 SETTLEMENT_UNIT:SETTLEMENT_UNIT,CONTAINER_TYPE:CONTAINER_TYPE,
     				 TAX_PRICE:TAX_PRICE,CONTAINER_NUM:CONTAINER_NUM,EXCHANGE_RATE:EXCHANGE_RATE,REMARK:REMARK,
     				 BILL_CODE:BILL_CODE,LADING_BILL_CODE:LADING_BILL_CODE
     			 },
     			 method: 'POST',
     			success: function () {
                    Ext.MessageBox.alert("保存成功","保存成功");
//	                       var grid = Ext.getCmp('grid');
						Ext.getCmp("startDate1").setValue(null); 
						Ext.getCmp("startDate2").setValue(null); 
						Ext.getCmp("startDate3").setValue(null); 
						Ext.getCmp("startDate4").setValue(null); 
						Ext.getCmp("startDate5").setValue(null); 
						Ext.getCmp("startDate6").setValue(null); 
						Ext.getCmp("startDate7").setValue(null);  
						Ext.getCmp("startDate9").setValue(null); 
					 gridOut.store.reload();
                },
                failure : function(response, request) {
             	   Ext.MessageBox.alert('错误提示', '请求' + request.url + '出错');
             	   }
     		 });
  	}},
  	{xtype: "button",minWidth:70,icon:'../plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png', text: "费用完成",handler:function(){
 		 Ext.Ajax.request({
  			 url: '../consignHandle/editState',
   			 params:{ 
   				BILL_CODE:billCode,
   				 IS_FEE_FINISH:"1"
   			 },
   			 method: 'POST',
   			success: function () {
                  Ext.MessageBox.alert("保存成功","保存成功");
//                      var grid = Ext.getCmp('grid');
					Ext.getCmp("startDate1").setValue(null); 
					Ext.getCmp("startDate2").setValue(null); 
					Ext.getCmp("startDate3").setValue(null); 
					Ext.getCmp("startDate4").setValue(null); 
					Ext.getCmp("startDate5").setValue(null); 
					Ext.getCmp("startDate6").setValue(null); 
					Ext.getCmp("startDate7").setValue(null);  
					Ext.getCmp("startDate9").setValue(null); 
					 grid.store.reload();
              },
              failure : function(response, request) {
           	   Ext.MessageBox.alert('错误提示', '请求' + request.url + '出错');
           	   }
   		 });
	}}
        ]},
//        {layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 2/5,  padding: '10px'}
    ]
});


var selFormOut = Ext.create('Ext.form.Panel', {
    title:'托单应付费用',
    name: 'selFormOut',
//    renderTo: "selFormOut",
    //collapsed :true,
    height:140,
    collapsible:true,
    bodyStyle: {
        padding: '5px',
        border: 1
    },
    fieldDefaults: {
        labelAlign: 'left'
    },
    items: [c1Out, c2Out,bsOut]
});
var selFormOut2 = Ext.create('Ext.form.Panel', {
//    title:'托单应付费用',
    name: 'selFormOut2',
//    renderTo: "selFormOut",
    //collapsed :true,
    height:50,
//    collapsible:true,
//    bodyStyle: {
//        padding: '5px',
//        border: 1
//    },
//    fieldDefaults: {
//        labelAlign: 'left'
//    },
    items: [cc4]

});
function allAmountOut(){
	var aa1= gridOut.getStore()
	var RMB1 = 0;
	var USD1 = 0;
	var zehe = 0;
	for(var i=0;i<aa1.getCount();i++)
	{
		if(gridOut.getStore().data.items[i].data.CURRENCY=='RMB'){
			RMB1+=parseInt(gridOut.getStore().data.items[i].data.TAX_PRICE)*parseInt(gridOut.getStore().data.items[i].data.CONTAINER_NUM);
		}else if(gridOut.getStore().data.items[i].data.CURRENCY=='USD'){
			USD1+=parseInt(gridOut.getStore().data.items[i].data.TAX_PRICE)*parseInt(gridOut.getStore().data.items[i].data.CONTAINER_NUM);
		}
		zehe+=parseInt(gridOut.getStore().data.items[i].data.EQUIVALENT_RMB);
			
//	   comValue+=parseInt(grid.getStore().data.items[i].data.TAX_PRICE);
	   
	  
	}
	
	Ext.getCmp("startDate621").setValue(RMB1); 
	Ext.getCmp("startDate721").setValue(USD1); 
	Ext.getCmp("startDate921").setValue(zehe); 
//	 alert(RMB);
//	   alert(USD);
}